"""
本模块定义了数据库相关方法
"""
import pymysql
from pymysql.cursors import DictCursor

from flask import request, current_app

from package.config import DBModel

class DB():
    def __init__(self, config: DBModel) -> None:
        self.DB_CONFIG = config.model_dump()

    def get_db_conn(self):
        """连接数据库"""
        conn = pymysql.connect(cursorclass=DictCursor ,**self.DB_CONFIG)
        return conn

    def test_connect(self):
        """测试数据库连接"""
        con = pymysql.connect(**self.DB_CONFIG)
        try:
            with con.cursor() as cursor:
                cursor.execute("SELECT VERSION()")
                data = cursor.fetchone()
                current_app.logger.info("Database version : %s " % data)
                return {"status": "success", "database_version": data[0]}
        finally:
            con.close()

def query():
    pass

def insert():
    pass